
/*************************************************************************
* Copyright (c) 2008, ShenZhen Coship Electronic Ltd Co.
* All rights reserved.
* 
* File Name:		DXSoundCapture.h
* Summary:			Capture audio data from MIC 
* 
* Current Version:	1.0
* Author(s):
/------------------------------------------------------------------------/
revision			author				reason					date
1.0					Ding Ning			Initialize Version		2008-6-2
/------------------------------------------------------------------------/
*************************************************************************/

#ifndef _DX_SOUND_CAPTURE_H_
#define _DX_SOUND_CAPTURE_H_
#pragma once
#include <mmsystem.h>
#include <dsound.h>
#define NUM_REC_NOTIFICATIONS  16
#include "../inc/DecBuf.h"
// #include "SysTrace.h"
#include "mystring.h"

class CDXSoundCapture
{
public:
	BOOL						m_bRecording;				//recording now ? also used by event recv thread 
protected:
	LPDIRECTSOUNDCAPTURE		m_pCapDev;					//capture device ptr
	LPDIRECTSOUNDCAPTUREBUFFER	m_pCapBuf;					//capture loop buffer ptr
	LPDIRECTSOUNDNOTIFY			m_pNotify;					//capture auto-notify event callback handler ptr
	GUID						m_guidCapDevId;				//capture device id
	WAVEFORMATEX				m_wfxInput;					//input wave format description struct 
	DSBPOSITIONNOTIFY			m_aPosNotify[NUM_REC_NOTIFICATIONS + 1];	//notify flag array 
	HANDLE						m_hNotifyEvent;				//notify event 
	BOOL						m_abInputFmtSupported[20];
	DWORD						m_dwCapBufSize;				//capture loop buffer size 
	DWORD						m_dwNextCapOffset;			//offset in loop buffer 
	DWORD						m_dwNotifySize;				//notify pos when loop buffer need to emit the event
	CircleBuf*					m_frame_handler;			// outer frame data dealer ptr 
	int							m_audioVolume;
public:				// callback func to add enum devices string name 
	static BOOL CALLBACK	enum_dev_proc(LPGUID lpGUID, LPCTSTR lpszDesc, LPCTSTR lpszDrvName, LPVOID lpContext ) ; 
	static ULONG WINAPI		notify_capture_thd(LPVOID data) ;
protected:
	HRESULT					InitDirectSound(GUID dev_id = GUID_NULL) ; 
	HRESULT					FreeDirectSound() ; 
	HRESULT					InitNotifications() ; 
	HRESULT					CreateCaptureBuffer(WAVEFORMATEX * wfx) ; 
	HRESULT					StartOrStopRecord(BOOL bStartRec) ;
	HRESULT					RecordCapturedData() ; 
	void					SetWavFormat(WAVEFORMATEX * wfx) ; 
public:
	CDXSoundCapture(void);
	~CDXSoundCapture(void);
	BOOL					Open(void) ; 
	BOOL					Close() ; 
	void					GrabAudioFrames(BOOL bGrabAudioFrames, CircleBuf* frame_handler) ; 
	void					SetVolume(int audioVolume);
	int						GetVolume();
};
#endif
